<template>
  <el-dialog
    :title="uploadType === 1 ? '上传会员图标' : '上传卡片景图'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-tabs v-model="activeName" >
        <el-tab-pane label="上传文件" name="uploadFile">
          <el-upload
            class="avatar-uploader"
            :action="uploadAction"
            :show-file-list="false"
            :on-success="handleSuccess"
            :before-upload="beforeUpload"
          >
            <img v-if="localImage" :src="localImage" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-tab-pane>
        <el-tab-pane label="在线文件" name="onlineFile">
            <ul>
                <div :title="imgInfo.title" class="clearfix image-info" v-for="(imgInfo,index) in foldingImgList" :key="index" @click.stop="imageHander(index)">
                    <li>
                        <img :src="imgInfo.imgUrl" :alt="imgInfo.alt" width="148px" height="148px"/>
                    </li>
                    <el-button v-if="index === currentImageIndex" class="el-button-position" type="success" icon="el-icon-check" circle></el-button>
                </div>
            </ul>
        </el-tab-pane>
    </el-tabs>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            localImage:'',
            visible: false,
            imageUrl:'',
            activeName:'uploadFile',
            uploadAction:'',
            foldingImgList:[
                {title:'测试图片',alt:'测试图片',imgUrl:'http://oss.xqtinfo.cn/20190817/545e2a54693e4dd0b71f2f7ca540f710.jpg'},
                {title:'测试图片',alt:'测试图片',imgUrl:'http://oss.xqtinfo.cn/20190826/00f7c9e39ca7442ca85d79bff26f9a34.jpeg'},
                {title:'测试图片',alt:'测试图片',imgUrl:'http://oss.xqtinfo.cn/20190826/f03e7c9da71e41789808a7414618445b.jpeg'}
            ],
            currentImageIndex:-1
        }
    },
    props:{
        uploadType:{
            type:Number,
            default() {
                return 1
            }
        }
    },
    methods:{
        init(type) {
            this.getImagesList(type);
            this.uploadAction = this.$http.adornUrl(
                `/sys/oss/upload?token=${this.$cookie.get("token")}`
            );
            this.visible = true;
        },
        confirm() {
          this.visible = false;
          if (this.activeName == "uploadFile") {
            this.$emit('handleSuccess', this.localImage, this.uploadType)
          } else {
            this.$emit('handleSuccess', this.imageUrl, this.uploadType)
          }
        },
        beforeUpload(file) {
            const isJPG = file.type === "image/jpeg";
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message.error("上传头像图片只能是 JPG 格式!");
            }
            if (!isLt2M) {
                this.$message.error("上传头像图片大小不能超过 2MB!");
            }
            return isJPG && isLt2M;
        },
        handleSuccess(file) {
          this.localImage = file.url;
        },
        getImagesList (type) {
          if (type * 1 == 1) {
            let icon = [{
              title:"1",
              alt:"1",
              imgUrl:"https://img.icons8.com/nolan/64/000000/membership-card.png"
            },
              {
                title:"1",
                alt:"1",
                imgUrl:"https://img.icons8.com/ultraviolet/80/000000/membership-card.png"
              },
              {
                title:"1",
                alt:"1",
                imgUrl:"https://i.52112.com/icon/jpg/256/20190611/43646/2052989.jpg"
              },
              {
                title:"1",
                alt:"1",
                imgUrl:"https://i.52112.com/icon/jpg/256/20180130/11995/603448.jpg"
              },
              {
                title:"1",
                alt:"1",
                imgUrl:"https://i.52112.com/icon/jpg/256/20190611/43647/2048347.jpg"
              }
            ];
            this.foldingImgList = icon;
            return ;
          }
          this.$http({
            url: this.$http.adornUrl('/sys/oss/list'),
            method: 'get',
            params: this.$http.adornParams({
              'page': 1,
              'limit': 10
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.foldingImgList = data.page.list.map(item => ({
                title:item.url.split('/')[4],
                alt:item.url.split('/')[4],
                imgUrl:item.url
              }))
            }
          })
        },
        imageHander(index) {
            this.currentImageIndex = index;
            this.imageUrl = this.foldingImgList[this.currentImageIndex].imgUrl
           // this.$emit('handleSuccess',this.imageUrl,this.uploadType)
        }
    }

};
</script>
<style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}

.clearfix {
    zoom: 1

}

.clearfix:after {
    content: "";
    display: block;
    height: 0;
    visibility: hidden;
    clear: both;
}

.image-info{
    display: inline-block;
}

.el-button-position{
    position: absolute;
    right: 10px;
    bottom: 10px;
}

.el-button.is-circle{
    padding: 4px;
}

ul div{
    position: relative;
}

ul div li {
    width:148px;
    height:148px;
    cursor: pointer;
    list-style:none;
    position:relative;
    float:left;
    margin:10px;
    overflow:hidden;
}
</style>
